<template src="./template.html"></template>

<script>
  import * as Api from '../../../api/rightsManagement';
  import Pagination from '@/components/Pagination' // secondary package based on el-pagination
  import {getService} from '@/api/supplierManagement/project';
  import AddEquity from './addEquity';
  import AddLevel from './addLevel';
  import AddService from './addService';
  import EditEquity from './editEquity';
  import moment from "moment";

  export default {
    name: 'bizRi',
    components: {Pagination, AddEquity, AddLevel, AddService, EditEquity},
    data() {
      return {
        listQuery: {
          equityType: null, // 权益类型
          equityID: null,
          equityLevelID: null, // 权益级别ID
          serviceID: null, // 服务ID
          pageNum: 1,
          pageSize: 20
        },
        gradeOptions: [
          {
            label: '保单权益',
            value: 1
          },
          {
            label: '客户权益',
            value: 2
          }
        ],
        equityArray: [],
        equityLevelArr: [],
        equityOptions: [], // 权益级别
        equityLevelOptions: [],
        allServiceList: [],
        serviceOptions: [],
        allServiceData: {},
        listLoading: false,
        tableData: [],
        dataCount: 0,
        // addStep: 0,// 新增向导步骤
        showAddEquity: false,
        showAddLevel: false,
        showAddService: false,
        showEditEquity: false,
        stepData: {},
        currentEquity: {}
      }
    },
    computed: {
      addStep: {
        set(value) {
          if (value === 0) {
            this.stepData = null;
          }
          this.showAddEquity = (value === 1);
          this.showAddLevel = (value === 2);
          this.showAddService = (value === 3);
        },
        get() {
          if (this.showAddEquity) return 1;
          if (this.showAddLevel) return 2;
          if (this.showAddService) return 3;
          return 0;
        }
      }
    },
    created() {
      // 获取所有服务
      getService().then(res => {
        this.allServiceList = res;
        this.serviceOptions = res;
        let data = {};
        res.forEach(s => data[s.serviceID] = s);
        this.allServiceData = data;
      });
      this.queryPageData();
      this.getEquityLevel();
    },
    methods: {
      addNextStep(data) {
        // console.log('step data:::', data);
        this.addStep++;
        this.stepData = {...this.stepData, ...data};
        console.log('step data:::::', this.stepData);
      },
      addPreviousStep() {
        this.addStep--;
      },
      addSaved() {
        this.addStep = 0;
        this.queryPageData();
      },
      queryPageData() {
        this.listLoading = true
        Api.getPageEquity(this.listQuery).then(response => {
          this.tableData = response.content
          this.dataCount = response.totalElements
          this.listLoading = false
        }).catch(() => {
          this.listLoading = false
        })
      },
      // 权益级别
      getEquityLevel() {
        let params = {
          equityID: this.listQuery.equityID,
          equityType: this.listQuery.equityType
        }
        Api.getEquityLevel(params).then(resp => {
          let equity = {};
          resp.forEach(e => {
            if (!equity[e.equityID]) {
              equity[e.equityID] = {
                equityName: e.equityName,
                equityID: e.equityID,
                equityType: e.equityType,
                equityLevel: []
              };
            }
            equity[e.equityID].equityLevel.push({equityLevelID: e.equityLevelID, equityLevelName: e.equityLevelName});
          })
          // console.log('object', Object.values(equity));
          this.equityArray = Object.values(equity);
          this.equityLevelArr = resp
          this.equityOptions = Object.values(equity);
        })
      },
      changeEquityType(val) {
        // console.log(val, this.equityArray);
        if (val) {
          this.listQuery.equityType = val;
          this.listQuery.equityID = null;
          this.listQuery.equityLevelID = null;
          this.equityOptions = this.equityArray.filter(e => {
            // console.log(e.equityType, val);
            return e.equityType === parseInt(val);
          });
          this.equityLevelOptions = [];
        } else {
          this.listQuery.equityType = null;
          this.equityOptions = this.equityArray;
          // this.listQuery.equityID = null;
          // this.listQuery.equityLevelID = null;
        }
      },
      changeEquity(val) {
        this.listQuery.equityLevelID = null;
        if (val) {
          this.equityLevelOptions = this.equityOptions.find(e => e.equityID === val).equityLevel;
        } else {
          this.equityLevelOptions = [];
        }
      },
      changeEquityLevel(val) {

        this.serviceOptions = this.equityLevelArr.find(e => e.equityLevelID === val).serviceLevelDef;
      },
      editEquity(equity) {
        // console.log('edit::::', equity);
        equity.readOnly = false;
        this.currentEquity = equity;
        this.showEditEquity = true;
      },
      editFinish(refresh) {
        this.showEditEquity = false;
        if (refresh) {
          this.queryPageData();
        }
      },
      deleteEquity(equity) {
        console.log('delete::::', equity);
        this.$confirm(`是否确定删除权益 "${equity.equityName}" ?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(async () => {
          return Api.deleteEquityByID(equity.equityID);
        }).then(() => {
          this.queryPageData();
          this.msgSuccess("删除成功");
        })
      },
      formatTime(timeStr) {
        if (!timeStr) {
          return '';
        }
        return moment(timeStr).format('YYYY-MM-DD hh:mm:ss');
      },
      onRowClick(equity, column, event) {
        // console.log('click....', row);
        equity.readOnly = true;
        this.currentEquity = equity;
        this.showEditEquity = true;
      }
    }
  }
</script>

<style lang="scss" scoped src="./style.scss"></style>
